<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>HashChange试验</title>
</head>
<body>
  <div class="main">
    <p>前端三驾马车</p>
    <ul id="list">
      <li>angular</li>
      <li>vue</li>
      <li>react</li>
    </ul>
  </div>
  <script>
    // 全局监听popState事件
    window.onpopstate = function(event) {
      console.log('onpopstate函数的event：', event)
    }

    // 监听hashChange事件
    window.onhashchange = function(event) {
      console.log('onhashchange函数的event：', event)
    }

    // 点击按钮时改变state
    document.querySelector('#list').addEventListener('click', function(event) {
      if (event.target.nodeName == 'LI') {
        var content = event.target.innerHTML
        var _newState
        _newState = {
          url: location.origin + '/' + content,
          title: document.title,
          state: content
        }
        // 调用replaceState方法跳转
        window.history.pushState(_newState, '', '/' + content);
        console.log('打印一下路由：', content) //打印一下路由： angular vue react
      }
    })
  </script>
</body>
</html>